package com.mlan.userap.adapter;



import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mlan.userap.R;
import com.mlan.userap.component.FaceManager;
import com.mlan.userap.model.ChatEntry;
import com.mlan.userap.model.ChatGroupInfo;
import com.mlan.userap.untils.CommTool;
import com.mlan.userap.untils.ImageLoader;
import com.mlan.userap.untils.MsgHelper;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

    private List<ChatGroupInfo> chatEntries;
    private OnItemClickListener listener;

    private OnItemLongClickListener longClickListener;

    public interface OnItemClickListener {
        void onItemClick(ChatGroupInfo chatEntry);
    }

    public interface  OnItemLongClickListener{
        void onItemLongClick(ChatGroupInfo chatGroupInfo,View view,ViewHolder holder);
    }

    public String getGroupId(int Pos){
        if (chatEntries!=null){
            return chatEntries.get(Pos).getGroupId();
        }
        return null;
    }

    public ChatAdapter(List<ChatGroupInfo> chatEntries) {
        this.chatEntries = chatEntries;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setLongClickListener(OnItemLongClickListener longClickListener){
        this.longClickListener = longClickListener;
    }


    // 更新整个数据集
    public void updateData(List<ChatGroupInfo> newChatEntries) {
        this.chatEntries.clear();
        this.chatEntries.addAll(newChatEntries);
        notifyDataSetChanged();
    }

    // 添加单个项
    public void addItem(ChatGroupInfo chatEntry) {
        this.chatEntries.add(chatEntry);
        notifyItemInserted(chatEntries.size() - 1);
    }

    // 删除单个项
    public void removeItem(int position) {
        if (position >= 0 && position < chatEntries.size()) {
            chatEntries.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, chatEntries.size());
        }
    }

    // 更新未读消息数量
    public void updateUnreadCount(int position, int unreadCount) {
        if (position >= 0 && position < chatEntries.size()) {
            chatEntries.get(position).setUnread(unreadCount);
            notifyItemChanged(position);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chat_entry, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ChatGroupInfo entry = chatEntries.get(position);


        ImageLoader.show(holder.ivAvatar,entry.getGroupAvatar());


       // holder.ivAvatar.setImageResource(entry.getAvatarResId());
        holder.tvNickname.setText(entry.getGroupName());


        if(entry.getLastMsg()!=null){
            int msgType = entry.getLastMsg().getMsgType();
            if (msgType == 1) {
                holder.tvLastMessage.setGravity(Gravity.CENTER_VERTICAL);
                FaceManager.getInstance().emojiJudge(holder.tvLastMessage,entry.getLastMsg().getMsgContent());
            }
            else {
                holder.tvLastMessage.setText(MsgHelper.GetText(entry.getLastMsg()));
            }
            holder.tvLastMessage.setVisibility(View.VISIBLE);
            holder.tvDate.setText(CommTool.formatTimestamp(entry.getLastMsg().getMsgTicks()));
            holder.tvDate.setVisibility(View.VISIBLE);
        }
        else {

            holder.tvLastMessage.setVisibility(View.GONE);
            holder.tvDate.setVisibility(View.GONE);
        }


        // 未读消息数量
        if (entry.getUnread() > 0) {
            holder.tvUnreadCount.setText(String.valueOf(entry.getUnread()));
            holder.tvUnreadCount.setVisibility(View.VISIBLE);
        } else {
            holder.tvUnreadCount.setVisibility(View.GONE);
        }
//
//        // 免打扰图标
//        if (entry.isDnd()) {
//            holder.ivDnd.setVisibility(View.VISIBLE);
//        } else {
//            holder.ivDnd.setVisibility(View.GONE);
//        }

        // 设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(entry);
                }
            }
        });

        // 添加触摸监听以获取长按位置
        holder.itemView.setOnTouchListener(new View.OnTouchListener() {

            float touchX, touchY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    touchX = event.getRawX();
                    touchY = event.getRawY();
                    v.setTag(R.id.tag_touch_x, touchX);
                    v.setTag(R.id.tag_touch_y, touchY);
                }
                return false;
            }
        });

        // 设置长按事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener!=null){
                    longClickListener.onItemLongClick(entry,v,holder);
                }
                return true; // 消耗长按事件
            }
        });


    }



    @Override
    public int getItemCount() {
        return chatEntries.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvUnreadCount;
        TextView tvNickname;
        TextView tvLastMessage;
        TextView tvDate;
        ImageView ivDnd;

        ViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvUnreadCount = itemView.findViewById(R.id.tv_unread_count);
            tvNickname = itemView.findViewById(R.id.tv_nickname);
            tvLastMessage = itemView.findViewById(R.id.tv_last_message);
            tvDate = itemView.findViewById(R.id.tv_date);
            ivDnd = itemView.findViewById(R.id.iv_dnd);
        }
    }
}